//
//  RecommondViewController.m
//  jinritoutiao
//
//  Created by 汤环 on 2025/8/13.
//

#import "RecommondViewController.h"
#import "HeightPickerView.h"

@interface RecommondViewController ()

/// 身高选择器视图
@property (nonatomic, strong) HeightPickerView *heightPickerView;

/// 显示选中身高的标签
@property (nonatomic, strong) UILabel *resultLabel;

@end

@implementation RecommondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tabBarItem.title = @"推荐";
    self.tabBarItem.image = [UIImage imageNamed:@"recommond"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupHeightPicker];
}

/**
 * 设置身高选择器
 */
- (void)setupHeightPicker {
    // 添加标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 30)];
    titleLabel.text = @"请选择您的身高";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textColor = [UIColor blackColor];
    [self.view addSubview:titleLabel];
    
    // 创建身高选择器（默认身高170cm）
    CGFloat pickerHeight = 120;
    CGFloat pickerY = CGRectGetMaxY(titleLabel.frame) + 80;
    self.heightPickerView = [[HeightPickerView alloc] initWithFrame:CGRectMake(20, pickerY, self.view.bounds.size.width - 40, pickerHeight) 
                                                      initialHeight:170];
    
    // 自定义颜色（可选）
    self.heightPickerView.scaleLineColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0];
    self.heightPickerView.indicatorColor = [UIColor systemBlueColor];
    
    // 设置值变化回调
    __weak typeof(self) weakSelf = self;
    self.heightPickerView.valueChangedBlock = ^(NSInteger heightValue) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if (strongSelf) {
            strongSelf.resultLabel.text = [NSString stringWithFormat:@"当前选择的身高：%ld cm", (long)heightValue];
            NSLog(@"身高选择变化：%ld cm", (long)heightValue);
        }
    };
    
    [self.view addSubview:self.heightPickerView];
    
    // 添加结果显示标签
    self.resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.heightPickerView.frame) + 40, self.view.bounds.size.width - 40, 30)];
    self.resultLabel.text = @"当前选择的身高：170 cm";
    self.resultLabel.textAlignment = NSTextAlignmentCenter;
    self.resultLabel.font = [UIFont systemFontOfSize:16];
    self.resultLabel.textColor = [UIColor darkGrayColor];
    [self.view addSubview:self.resultLabel];
    
    // 添加确认按钮
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
    confirmButton.frame = CGRectMake(60, CGRectGetMaxY(self.resultLabel.frame) + 30, self.view.bounds.size.width - 120, 44);
    [confirmButton setTitle:@"确认选择" forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    confirmButton.backgroundColor = [UIColor systemBlueColor];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.layer.cornerRadius = 8;
    [confirmButton addTarget:self action:@selector(confirmButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    
    // 添加使用说明
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(confirmButton.frame) + 20, self.view.bounds.size.width - 40, 60)];
    tipLabel.text = @"左右滑动选择身高\n每滑动1cm会有震动反馈\n松手后会自动吸附到整数刻度";
    tipLabel.numberOfLines = 0;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:12];
    tipLabel.textColor = [UIColor lightGrayColor];
    [self.view addSubview:tipLabel];
}

/**
 * 确认按钮点击事件
 * @param sender 按钮对象
 */
- (void)confirmButtonTapped:(UIButton *)sender {
    NSInteger selectedHeight = self.heightPickerView.currentHeight;
    
    // 创建提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认身高" 
                                                                   message:[NSString stringWithFormat:@"您选择的身高是：%ld cm", (long)selectedHeight]
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" 
                                                       style:UIAlertActionStyleDefault 
                                                     handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"用户确认身高：%ld cm", (long)selectedHeight);
        // 在这里可以保存用户选择的身高或进行其他操作
    }];
    
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
